<?php
/**
 *　                  oooooooooooo
 *
 *                  ooooooooooooooooo
 *                       o
 *                      o
 *                     o        o
 *                    oooooooooooo
 *
 *         ～～         ～～         　　～～
 *       ~~　　　　　~~　　　　　　　　~~
 * ~~～~~～~~　　　~~~～~~～~~～　　　~~~～~~～~~～
 * ·······              ~~XYHCMS~~            ·······
 * ·······  闲看庭前花开花落 漫随天外云卷云舒 ·······
 * ·············     www.xyhcms.com     ·············
 * ··················································
 * ··················································
 *
 * @Author: gosea <gosea199@gmail.com>
 * @Date:   2014-06-21 10:00:00
 * @Last Modified by:   gosea
 * @Last Modified time: 2017-06-13 21:38:34
 */
namespace Home\Controller;

use Common\Lib\Category;

class ListController extends HomeCommonController
{

    public function index()
    {

        $cid   = I('cid', 0, 'intval');
        $ename = I('e', '', 'htmlspecialchars,trim');

        $cate = get_category();

        if (!empty($ename)) { //ename不为空
            $self = Category::getSelfByEName($cate, $ename); //当前栏目信息
        } else {
//$cid来判断

            $self = Category::getSelf($cate, $cid); //当前栏目信息
        }

        if (empty($self)) {
            $this->error('栏目不存在');
        }

        $cid         = $self['id']; //当使用ename获取的时候，就要重新给$cid赋值，不然0
        $_GET['cid'] = $cid; //栏目ID
        $self['url'] = get_url($self);

        //访问权限
        $groupid = intval(get_cookie('groupid'));
        $groupid = (empty($groupid) || empty($this->uid))? 1 : $groupid; //1为游客
        //判断访问权限
        $access = M('categoryAccess')->where(array('catid' => $cid, 'flag' => 0, 'action' => 'visit'))->getField('roleid', true);
        //权限存在，则判断
        if (!empty($access) && !in_array($groupid, $access)) {
            $this->error('您没有访问该信息的权限！');
        }

        $this->assign('cate', $self);
        $this->assign('flag_son', Category::hasChild($cate, $cid)); //是否包含子类
        $this->assign('title', empty($self['seotitle']) ? $self['name'] : $self['seotitle']);
        $this->assign('keywords', $self['keywords']);
        $this->assign('description', $self['description']);
        $this->assign('cid', $cid);

        // $patterns = array('/^List_/', '/'.C('TMPL_TEMPLATE_SUFFIX').'$/');
        $patterns      = array('/' . C('TMPL_TEMPLATE_SUFFIX') . '$/');
        $replacements  = array('');
        $template_list = preg_replace($patterns, $replacements, $self['template_list']);

        if (empty($template_list)) {
            $this->error('模板不存在');
        }

        switch ($self['tablename']) {
            case 'article':
                $this->display($template_list);
                return;
                break;
            case 'teacher':
                $this->display($template_list);
                return;
                break;
            case 'xqdj':
                //校企对接添加分类
                $industry = get_item('industry');
                $xqdj1 = get_item('xqdj1');
                $xqdj2 = get_item('xqdj2');
                $xqdj3 = get_item('xqdj3');
                // print_r($_POST);die;
                $this->assign('industry',$industry);
                $this->assign('xqdj1',$xqdj1);
                $this->assign('xqdj2',$xqdj2);
                $this->assign('xqdj3',$xqdj3);
                $this->getXqdjList();
                $this->display($template_list);
                return;
                break;
            case 'product':
                $this->display($template_list);
                return;
                break;
            case 'picture':
                $this->display($template_list);
                return;
                break;
            case 'soft':
                $this->display($template_list);
                return;
                break;
            case 'page':
                {
                    $cate            = M('Category')->Field('content')->find($cid);
                    $self['content'] = $cate['content'];
                    $this->assign('cate', $self);
                    $this->display($template_list);
                }
                return;
                break;
            case 'phrase':
                $this->display($template_list);
                return;
                break;
            default:
                //$this->error('参数错误');
                $userOther = A(ucfirst($self['tablename']));
                $userOther->lists();
                return;
                break;
        }

        $this->display();

    }

    public function map()
    {

        $this->assign('title', '网站地图');
        $this->assign('keywords', '网站地图');
        $this->assign('description', '网站地图');
        $this->display();
    }

    public function getXqdjList()
    {
        $industryid   = I('industryid', 0, 'intval');
        $xqdj1id   = I('xqdj1id', 0, 'intval');
        $xqdj2id   = I('xqdj2id', 0, 'intval');
        $xqdj3id   = I('xqdj3id', 0, 'intval');
        $keyword = I('keyword', '', 'htmlspecialchars,trim'); //关键字

        $where='1=1';
        if (!empty($keyword)) {
            // $where['title'] = array('LIKE', "%{$keyword}%");
            $where.= ' AND title LIKE '.'"%'.$keyword.'%"';
        }

        if($industryid){
            // $where['industryid'] = array('IN', $industryid);
            $where.= " AND industryid = $industryid";
        }

        if($xqdj1id){
            // $where['xqdj1id'] = array('IN', $xqdj1id);
            $where.= " AND xqdj1id = $xqdj1id";
        }

        if($xqdj2id){
            // $where['xqdj2id'] = array('IN', $xqdj2id);
             $where.= " AND xqdj2id = $xqdj2id";
        }

        if($xqdj3id){
            // $where['xqdj3id'] = array('IN', $xqdj3id);
            $where.= " AND xqdj3id = $xqdj3id";
        }
            // echo 12;die;
        $count = D2('ArcView', 'xqdj')->where($where)->count();

        $page           = new \Common\Lib\Page($count, 1);
        $page->rollPage = 4;
        $page->setConfig('prev',"上一页");
        $page->setConfig('next',"下一页");
        $page->setConfig('first',"首页");
        $page->setConfig('end',"尾页");
        $page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $limit = $page->firstRow . ',' . $page->listRows;

        $art   = D2('ArcView', 'xqdj')->where($where)->order('id DESC')->limit($limit)->select();
        foreach($art as $k=>$v)
        {
            $jumpflag = ($v['flag'] & B_JUMP) == B_JUMP? true : false;
            $art[$k]['url'] = get_content_url($v['id'],$v['cid'],$v['ename'],$jumpflag,$v['jumpurl']);
            $art[$k]['keywordArr'] = explode('，',$v['keywords']);
        }
        $this->assign('page', $page->show());
        $this->assign('vlist', $art);

        $this->assign('industryid',$industryid);
        $this->assign('xqdj1id',$xqdj1id);
        $this->assign('xqdj2id',$xqdj2id);
        $this->assign('xqdj3id',$xqdj3id);
        $this->assign('keyword',$keyword);
    }
}
